package com.example.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.common.BaseContext;
import com.example.common.R;
import com.example.entity.Employee;
import com.example.service.EmployeeService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.util.DigestUtils;
import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.nio.charset.StandardCharsets;
/**
 * 员工信息(Employee)表控制层
 *
 * @author makejava
 * @since 2022-05-02 19:09:15
 */
@Slf4j
@RestController
@RequestMapping("/employee")
public class EmployeeController {
    /**
     * 服务对象
     */
    @Resource
    private EmployeeService employeeService;

    @PostMapping("/login")
    public R<Employee> login(HttpServletRequest request,@RequestBody Employee employee) {
        String password= DigestUtils.md5DigestAsHex(employee.getPassword().getBytes());
        LambdaQueryWrapper<Employee> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Employee::getUsername,employee.getUsername());
        Employee emp = employeeService.getOne(queryWrapper);
        if(emp==null){
            return R.error("登陆失败");
        }
        if (!emp.getPassword().equals(password)){
            return R.error("登陆失败");
        }
        if (emp.getStatus()==0){
            return R.error("您已被封号");
        }
        request.getSession().setAttribute("employee",emp.getId());
        return R.success(emp);
    }

    /**
     * 员工退出
     * @param request
     * @return
     */
    @PostMapping("/logout")
    public R<String> logout(HttpServletRequest request){
        //清理当前session
        request.getSession().removeAttribute("employee");
        return R.success("退出成功");
    }
    //新增员工
    @PostMapping
    public R<String>save(HttpServletRequest request,@RequestBody Employee employee ){
        System.out.println("修改员工线程值为"+Thread.currentThread().getId());
        log.info("新增员工,员工信息{} ",employee.toString());
        employee.setPassword(DigestUtils.md5DigestAsHex("123456".getBytes(StandardCharsets.UTF_8)));
        employee.setStatus(1);
        employeeService.save(employee);
        return R.success("新增成功");
    }
    @GetMapping("/page")
    public R<Page> pageController(int page,int pageSize,String name){
        System.out.println(BaseContext.getThreadLocal()+"获取成功");
            log.info("page={},pagesize={},name={}",page,pageSize,name);
            //构造分页构造器
            Page pageinfo=new Page(page,pageSize);
            //构造条件构造器
            LambdaQueryWrapper<Employee > queryWrapper=new LambdaQueryWrapper();//构造条件构造器
            queryWrapper.like(StringUtils.isNotBlank(name),Employee::getName,name);//使用模糊查询
            //添加排序条件
            queryWrapper.orderByDesc(Employee::getUpdateTime);
            //执行查询
            employeeService.page(pageinfo,queryWrapper);
            return R.success(pageinfo);
    }
    @PutMapping
    public R<String> putEmployee(@RequestBody Employee employee,HttpServletRequest request){
        BaseContext.setThreadLocal((Long) request.getSession().getAttribute("employee"));
        LambdaQueryWrapper<Employee>queryWrapper=new LambdaQueryWrapper<>();
        queryWrapper.eq(Employee::getId,employee.getId());
        boolean a=employeeService.updateById(employee);
        if (a){
            return R.success("修改成功");
        }
        return R.error("未知错误");

    }
    @GetMapping("/{id}")
    public R<Employee> getEmployee(@PathVariable String id){
        Employee employee=employeeService.getById(Long.parseLong(id));
        if (employee!=null){
            return R.success(employee);
        }
        return R.error("没有找到相应的员工信息");

    }


}